package com.team24.hotel.controller;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.team24.hotel.domain.Hotel;
import com.team24.hotel.domain.Room;
import com.team24.hotel.domain.Supplier;
import com.team24.hotel.service.SupplierService;
import io.micrometer.core.instrument.Meter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.swing.*;
import java.awt.*;
import java.util.List;

@Controller
@RequestMapping("/supplier")
public class SupplierController {
    @Autowired
    private SupplierService supplierService;

    @GetMapping("/list")
    public String listSupplierPage(Model model) {
        List<Supplier> supplier = supplierService.listSupplier();
        model.addAttribute("supplier", supplier);
        return "supplierList";
    }

    @RequestMapping("toAdd")
    public String add(Model model) {
        model.addAttribute("supplier",new Supplier());
        return "supplierAdd";
    }

    @PostMapping("/save")
    public String saveSupplier(Supplier supplier) {
        int insert = supplierService.saveOrUpdate(supplier);
        if (insert > 0) {
            return "redirect:/supplier/list";
        } else {
            return "redirect: error";
        }
    }

    @RequestMapping("/delete")
    public String deleteSupplierById(Integer id){
        supplierService.deleteByPrimaryKey(id);
        return "redirect:/supplier/list";
    }

    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public String editSupplier(Supplier supplier) {
        supplierService.updateSupplierInfo(supplier);
        return "redirect:/supplier/list";
    }

    @RequestMapping(value = "/findById")
    public String findSupplierById(Model model,Integer id){
        Supplier supplier = supplierService.findSupplierById(id);
        model.addAttribute("supplier",supplier);
        return "supplierAdd";
    }
}



